import {createSlice} from '@reduxjs/toolkit';

//创建切片对象  (类比旧版 redux 中的 reducer.js)
let slice = createSlice({
  //名字
  name: 'count',
  //状态的初始值
  initialState: 10,
  //reducer 函数的功能
  reducers: {
    //新增的情况
    incre: (state, action) => {
      return state + action.payload;
    },
    //减少的情况
    decre: (state, action) => {
      return state - action.payload;
    }
  }
});

//获得 reducer 函数
export default slice.reducer;

//获得同步的 action creator   incre 和 decre 是同步的 action creator
export let {incre, decre} = slice.actions;

//异步更新状态  异步的 action creator
export let asyncIncreAction = (payload) => {
  return dispatch => {
    setTimeout(() => {
      dispatch(incre(payload));
    }, 1000)
  }
}